﻿using System;
using System.Threading.Tasks;
using System.Windows;

namespace Phoenix
{
    public abstract class PhoenixApplication : PhoenixApplicationHostTarget
    {
        // ReSharper disable EventNeverSubscribedTo.Local
        // ReSharper disable UnusedMember.Local
        // ReSharper disable EventNeverInvoked
        new Uri StartupUri { get; set; }
        new event StartupEventHandler Startup;
        // ReSharper restore EventNeverSubscribedTo.Local
        // ReSharper restore UnusedMember.Local
        // ReSharper restore EventNeverInvoked

        protected override void OnStartup(StartupEventArgs e)
        {
            var handler = Startup;
            if (handler != null) handler(this, e);
            InitialisePhoenix()
                .ContinueWith(t=> Host = t.Result);
        }

        protected override Task<IPhoenixHost> InitialisePhoenix()
        {
            var hostBuilder = new PhoenixHostBuilder(
                TaskScheduler.FromCurrentSynchronizationContext(),
                Dispatcher);
            ConfigurePhoenixHostBuilder(hostBuilder);
            return hostBuilder.Run(this);
        }

        protected abstract void ConfigurePhoenixHostBuilder(IPhoenixHostBuilder hostBuilder);
    }
}